package com.xiaoKe.zhonghzu.global.tool;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.lang.reflect.Field;

/**
 * @ProjectName : app
 * @Author : Shijingna
 * @Time : 2022/9/26 10:29 上午
 * @Description : 获取app内部信息
 */
public class PackageInfoUtils {
    public static int packageCode(Context context) {
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    public static String packageName(Context context) {
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }

    public static String packageId(Context context) {
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            name = info.sharedUserId;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }

    /**
     * 获取本地路径
     **/
    public static String getFilePathOfDat(String str) {
        String userId = SPUtils.getString("userId", "");
        return userId + str;
    }
    /**
     * TinkerVersion
     **/
    public static String getTinkerVersion(Context context) {
        try {
            // 通过类加载器加载Tinker的Version类
            Class<?> versionClass = Class.forName("com.tencent.tinker.loader.TinkerLoader");
            // 获取版本号字段
            Field field = versionClass.getDeclaredField("TINKERPATCH_VERSION");
            // 获取版本号值
            String version = (String) field.get(null);
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "Unknown";
        }
    }
}
